/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controladores;

import controladores.exceptions.IllegalOrphanException;
import controladores.exceptions.NonexistentEntityException;
import controladores.exceptions.PreexistingEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import mundo.Persona;
import mundo.Compra;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import mundo.Empleado;
import mundo.Venta;

/**
 *
 * @author USUARIO WINDOWS
 */
public class EmpleadoJpaController implements Serializable {

    public EmpleadoJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Empleado empleado) throws IllegalOrphanException, PreexistingEntityException, Exception {
        if (empleado.getCompraCollection() == null) {
            empleado.setCompraCollection(new ArrayList<Compra>());
        }
        if (empleado.getVentaCollection() == null) {
            empleado.setVentaCollection(new ArrayList<Venta>());
        }
        List<String> illegalOrphanMessages = null;
        Persona personaOrphanCheck = empleado.getPersona();
        if (personaOrphanCheck != null) {
            Empleado oldEmpleadoOfPersona = personaOrphanCheck.getEmpleado();
            if (oldEmpleadoOfPersona != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("The Persona " + personaOrphanCheck + " already has an item of type Empleado whose persona column cannot be null. Please make another selection for the persona field.");
            }
        }
        if (illegalOrphanMessages != null) {
            throw new IllegalOrphanException(illegalOrphanMessages);
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Persona persona = empleado.getPersona();
            if (persona != null) {
                persona = em.getReference(persona.getClass(), persona.getIdPersona());
                empleado.setPersona(persona);
            }
            Collection<Compra> attachedCompraCollection = new ArrayList<Compra>();
            for (Compra compraCollectionCompraToAttach : empleado.getCompraCollection()) {
                compraCollectionCompraToAttach = em.getReference(compraCollectionCompraToAttach.getClass(), compraCollectionCompraToAttach.getIdCompra());
                attachedCompraCollection.add(compraCollectionCompraToAttach);
            }
            empleado.setCompraCollection(attachedCompraCollection);
            Collection<Venta> attachedVentaCollection = new ArrayList<Venta>();
            for (Venta ventaCollectionVentaToAttach : empleado.getVentaCollection()) {
                ventaCollectionVentaToAttach = em.getReference(ventaCollectionVentaToAttach.getClass(), ventaCollectionVentaToAttach.getIdVenta());
                attachedVentaCollection.add(ventaCollectionVentaToAttach);
            }
            empleado.setVentaCollection(attachedVentaCollection);
            em.persist(empleado);
            if (persona != null) {
                persona.setEmpleado(empleado);
                persona = em.merge(persona);
            }
            for (Compra compraCollectionCompra : empleado.getCompraCollection()) {
                Empleado oldEmpleadoidPersonaOfCompraCollectionCompra = compraCollectionCompra.getEmpleadoidPersona();
                compraCollectionCompra.setEmpleadoidPersona(empleado);
                compraCollectionCompra = em.merge(compraCollectionCompra);
                if (oldEmpleadoidPersonaOfCompraCollectionCompra != null) {
                    oldEmpleadoidPersonaOfCompraCollectionCompra.getCompraCollection().remove(compraCollectionCompra);
                    oldEmpleadoidPersonaOfCompraCollectionCompra = em.merge(oldEmpleadoidPersonaOfCompraCollectionCompra);
                }
            }
            for (Venta ventaCollectionVenta : empleado.getVentaCollection()) {
                Empleado oldEmpleadoidPersonaOfVentaCollectionVenta = ventaCollectionVenta.getEmpleadoidPersona();
                ventaCollectionVenta.setEmpleadoidPersona(empleado);
                ventaCollectionVenta = em.merge(ventaCollectionVenta);
                if (oldEmpleadoidPersonaOfVentaCollectionVenta != null) {
                    oldEmpleadoidPersonaOfVentaCollectionVenta.getVentaCollection().remove(ventaCollectionVenta);
                    oldEmpleadoidPersonaOfVentaCollectionVenta = em.merge(oldEmpleadoidPersonaOfVentaCollectionVenta);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findEmpleado(empleado.getIdPersona()) != null) {
                throw new PreexistingEntityException("Empleado " + empleado + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Empleado empleado) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Empleado persistentEmpleado = em.find(Empleado.class, empleado.getIdPersona());
            Persona personaOld = persistentEmpleado.getPersona();
            Persona personaNew = empleado.getPersona();
            Collection<Compra> compraCollectionOld = persistentEmpleado.getCompraCollection();
            Collection<Compra> compraCollectionNew = empleado.getCompraCollection();
            Collection<Venta> ventaCollectionOld = persistentEmpleado.getVentaCollection();
            Collection<Venta> ventaCollectionNew = empleado.getVentaCollection();
            List<String> illegalOrphanMessages = null;
            if (personaNew != null && !personaNew.equals(personaOld)) {
                Empleado oldEmpleadoOfPersona = personaNew.getEmpleado();
                if (oldEmpleadoOfPersona != null) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("The Persona " + personaNew + " already has an item of type Empleado whose persona column cannot be null. Please make another selection for the persona field.");
                }
            }
            for (Compra compraCollectionOldCompra : compraCollectionOld) {
                if (!compraCollectionNew.contains(compraCollectionOldCompra)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Compra " + compraCollectionOldCompra + " since its empleadoidPersona field is not nullable.");
                }
            }
            for (Venta ventaCollectionOldVenta : ventaCollectionOld) {
                if (!ventaCollectionNew.contains(ventaCollectionOldVenta)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Venta " + ventaCollectionOldVenta + " since its empleadoidPersona field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (personaNew != null) {
                personaNew = em.getReference(personaNew.getClass(), personaNew.getIdPersona());
                empleado.setPersona(personaNew);
            }
            Collection<Compra> attachedCompraCollectionNew = new ArrayList<Compra>();
            for (Compra compraCollectionNewCompraToAttach : compraCollectionNew) {
                compraCollectionNewCompraToAttach = em.getReference(compraCollectionNewCompraToAttach.getClass(), compraCollectionNewCompraToAttach.getIdCompra());
                attachedCompraCollectionNew.add(compraCollectionNewCompraToAttach);
            }
            compraCollectionNew = attachedCompraCollectionNew;
            empleado.setCompraCollection(compraCollectionNew);
            Collection<Venta> attachedVentaCollectionNew = new ArrayList<Venta>();
            for (Venta ventaCollectionNewVentaToAttach : ventaCollectionNew) {
                ventaCollectionNewVentaToAttach = em.getReference(ventaCollectionNewVentaToAttach.getClass(), ventaCollectionNewVentaToAttach.getIdVenta());
                attachedVentaCollectionNew.add(ventaCollectionNewVentaToAttach);
            }
            ventaCollectionNew = attachedVentaCollectionNew;
            empleado.setVentaCollection(ventaCollectionNew);
            empleado = em.merge(empleado);
            if (personaOld != null && !personaOld.equals(personaNew)) {
                personaOld.setEmpleado(null);
                personaOld = em.merge(personaOld);
            }
            if (personaNew != null && !personaNew.equals(personaOld)) {
                personaNew.setEmpleado(empleado);
                personaNew = em.merge(personaNew);
            }
            for (Compra compraCollectionNewCompra : compraCollectionNew) {
                if (!compraCollectionOld.contains(compraCollectionNewCompra)) {
                    Empleado oldEmpleadoidPersonaOfCompraCollectionNewCompra = compraCollectionNewCompra.getEmpleadoidPersona();
                    compraCollectionNewCompra.setEmpleadoidPersona(empleado);
                    compraCollectionNewCompra = em.merge(compraCollectionNewCompra);
                    if (oldEmpleadoidPersonaOfCompraCollectionNewCompra != null && !oldEmpleadoidPersonaOfCompraCollectionNewCompra.equals(empleado)) {
                        oldEmpleadoidPersonaOfCompraCollectionNewCompra.getCompraCollection().remove(compraCollectionNewCompra);
                        oldEmpleadoidPersonaOfCompraCollectionNewCompra = em.merge(oldEmpleadoidPersonaOfCompraCollectionNewCompra);
                    }
                }
            }
            for (Venta ventaCollectionNewVenta : ventaCollectionNew) {
                if (!ventaCollectionOld.contains(ventaCollectionNewVenta)) {
                    Empleado oldEmpleadoidPersonaOfVentaCollectionNewVenta = ventaCollectionNewVenta.getEmpleadoidPersona();
                    ventaCollectionNewVenta.setEmpleadoidPersona(empleado);
                    ventaCollectionNewVenta = em.merge(ventaCollectionNewVenta);
                    if (oldEmpleadoidPersonaOfVentaCollectionNewVenta != null && !oldEmpleadoidPersonaOfVentaCollectionNewVenta.equals(empleado)) {
                        oldEmpleadoidPersonaOfVentaCollectionNewVenta.getVentaCollection().remove(ventaCollectionNewVenta);
                        oldEmpleadoidPersonaOfVentaCollectionNewVenta = em.merge(oldEmpleadoidPersonaOfVentaCollectionNewVenta);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = empleado.getIdPersona();
                if (findEmpleado(id) == null) {
                    throw new NonexistentEntityException("The empleado with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Empleado empleado;
            try {
                empleado = em.getReference(Empleado.class, id);
                empleado.getIdPersona();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The empleado with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Compra> compraCollectionOrphanCheck = empleado.getCompraCollection();
            for (Compra compraCollectionOrphanCheckCompra : compraCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Empleado (" + empleado + ") cannot be destroyed since the Compra " + compraCollectionOrphanCheckCompra + " in its compraCollection field has a non-nullable empleadoidPersona field.");
            }
            Collection<Venta> ventaCollectionOrphanCheck = empleado.getVentaCollection();
            for (Venta ventaCollectionOrphanCheckVenta : ventaCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Empleado (" + empleado + ") cannot be destroyed since the Venta " + ventaCollectionOrphanCheckVenta + " in its ventaCollection field has a non-nullable empleadoidPersona field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Persona persona = empleado.getPersona();
            if (persona != null) {
                persona.setEmpleado(null);
                persona = em.merge(persona);
            }
            em.remove(empleado);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Empleado> findEmpleadoEntities() {
        return findEmpleadoEntities(true, -1, -1);
    }

    public List<Empleado> findEmpleadoEntities(int maxResults, int firstResult) {
        return findEmpleadoEntities(false, maxResults, firstResult);
    }

    private List<Empleado> findEmpleadoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Empleado.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Empleado findEmpleado(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Empleado.class, id);
        } finally {
            em.close();
        }
    }

    public int getEmpleadoCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Empleado> rt = cq.from(Empleado.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
